/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dk.netl.dashboard.widgets.alivewidget;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author g36267
 */
public class ServerInfoImpl implements ServerInfo {

    private static final String PATH_TO_BUILD_INFO = "netl" + "/" + "buildinfo.js";
    private String serverURL;
    private String serverName;
    private int index;

    public ServerInfoImpl(int index, String serverName, String serverURL) {
        this.serverURL = serverURL;
        this.serverName = serverName;
        this.index = index;
    }

    @Override
    public boolean isAlive() {
        return getBuildInfoFile() != null;
    }

    @Override
    public String getName() {
        return serverName;
    }

    @Override
    public int getIndex() {
        return index;
    }

    @Override
    public Date getLastDeployDate() {
        Date date = null;
        try {
            String buildInfo = getBuildInfoFile();
            if (buildInfo == null) {
                return null;
            }
            int startIdx = buildInfo.indexOf("deployed: ") + "deployed: ".length();
            int endIdx = buildInfo.indexOf("';");
            String dateString = buildInfo.substring(startIdx, endIdx);
            DateFormat format = new SimpleDateFormat("dd/MM-yy hh:mm");
            date = format.parse(dateString);
        } catch (ParseException ex) {
            Logger.getLogger(ServerInfoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return date;
    }

    @Override
    public String getVersion() {
        String buildInfo = getBuildInfoFile();
        if (buildInfo == null) {
            return null;
        }
        int startIdx = buildInfo.indexOf("version: ") + "version: ".length();
        int endIdx = buildInfo.indexOf(", build");
        return buildInfo.substring(startIdx, endIdx);
    }

    private String getBuildInfoFile() {
        BufferedReader br = null;
        try {
            URL url = new URL(serverURL + "/" + PATH_TO_BUILD_INFO);
            br = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder builder = new StringBuilder();
            String inputLine;
            while ((inputLine = br.readLine()) != null) {
                builder.append(inputLine);
            }
            return builder.toString();
        } catch (Exception ex) {
//            Logger.getLogger(ServerInfoImpl.class.getName()).log(Level.INFO, null, ex);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
//                Logger.getLogger(ServerInfoImpl.class.getName()).log(Level.INFO, null, ex);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        ServerInfo serverInfo = new ServerInfoImpl(0, "aite","http://cc-t-d134-c01-s02.cr.root4.net:7501/bilbog-server");
        System.out.println(serverInfo.getLastDeployDate());
        System.out.println(serverInfo.getVersion());
    }
}
